<template>
  <div class="chat-main">
    <div class="chat" id="chat" ref="refAiMain">
      <!-- 聊天内容 -->
      <div class="chat-content" v-for="(item, index) in chatList" :key="index">
        <div class="flex-end" v-if="item.role === 'user' && index !==1 ">
          <div class="flex-col">
            <span class="chat-my">{{ item.content }}</span>
          </div>
          <img src="./tx.png" class="avatar user-avatar" />
        </div>
        <div class="flex-start ai-box" v-else-if="item.role === 'assistant'">
          <img src="./aiUser.png" class="avatar ai-avatar" />
          <div class="ai-message">
            <span class="chat-ai" v-loading="item?.aiLoad">{{ item.content }}</span>
            <span v-if="item.stopped" class="stopped-message">这条消息已停止</span>
            <div class="message-actions" v-if="outputEnd && !item.stopped">
              <span class="action-text">本回答由 AI 生成，内容仅供参考，请谨慎甄别。</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 输入区域和发送按钮 -->
    <div class="input-container">
      <el-input
        type="textarea"
        :autosize="{ minRows: 4, maxRows: 8 }"
        placeholder="请输入您的问题"
        v-model="inputValue"
        @keyup.enter="onSend"
        :disabled="aiLoad"
      ></el-input>
      <el-icon
          :disabled="!outputEnd && !aiLoad"
          class="send-button"
          :class="{ disabledStyle: !outputEnd && !aiLoad }"
          @click="onSend"
          :size="40"
          :color="outputEnd ? '#008f5f' : '#ff0000'"
      >
        <Promotion v-if="outputEnd"/>
        <VideoPause v-else/>
      </el-icon>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ElMessage } from 'element-plus'
import { ref, nextTick } from 'vue'
import {
  Promotion,
  VideoPause
} from '@element-plus/icons-vue'
const props = defineProps({
  chatList: {
    type: Array,
    required: true
  },
  outputEnd: {
    type: Boolean,
    required: true
  },
  aiLoad: {
    type: Boolean,
    required: true
  }
})
const emit = defineEmits([
  'send',
  'stopOutput'
])

const inputValue = ref('')
const refAiMain = ref(null)

// 发送消息
const onSend = () => {
  if (props.outputEnd) {
    if (!inputValue.value || inputValue.value.trim().length === 0) {
      ElMessage.error('请输入问题')
      return
    }
    emit('send', [{
      content:inputValue.value,
      role:'user'
    }])
    inputValue.value = ''
  }else{
    // 如果输出未结束，点击暂停按钮停止输出
    emit('stopOutput')
  }
}

// 滚动到底部
const scrollToBottom = () => {
  if (!refAiMain.value) return
  setTimeout(() => {
    refAiMain.value.scrollTop = refAiMain.value.scrollHeight
  }, 100)
}

defineExpose({
  scrollToBottom,
})
</script>

<style scoped lang="scss">
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.flex-between {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
}

.flex-end {
  display: flex;
  justify-content: flex-end;
  align-items: flex-start;
}

.flex-col {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-end;
  max-width: 90%;
  border-radius: 6px;
}

.flex-start {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
}

.user-avatar {
  margin-left: 10px;
}

.ai-avatar {
  margin-right: 10px;
}

.ai-message {
  display: flex;
  flex-direction: column;
  max-width: 90%;
}

.message-actions {
  display: flex;
  align-items: center;
  margin-top: 8px;
  font-size: 12px;
  color: #909399;

  .action-icon {
    margin-right: 12px;
    cursor: pointer;

    &:hover {
      color: #008f5f;
    }
  }

  .action-divider {
    width: 1px;
    height: 12px;
    background-color: #dcdfe6;
    margin: 0 10px;
  }

  .action-text {
    color: #909399;
  }
}

.chat {
  overflow-y: auto;
  margin: 0 0 10px 0;
  padding: 15px;
  height: calc(100% - 120px);
  border: 1px solid #e4e7ed;
  background: #fff;
  border-radius: 8px !important;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);

  // 空聊天界面样式
  .empty-chat {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #606266;
    text-align: center;
    padding: 0 20px;

    .empty-chat-icon {
      margin-bottom: 20px;
      background: transparent;
      width: 100px;
      height: 100px;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    .empty-chat-title {
      font-size: 24px;
      color: #303133;
      margin-bottom: 10px;
      font-weight: 500;
    }

    .empty-chat-desc {
      font-size: 16px;
      color: #606266;
      margin-bottom: 30px;
    }

    .empty-chat-examples {
      width: 100%;
      max-width: 600px;

      .example-title {
        font-size: 16px;
        color: #303133;
        margin-bottom: 15px;
        font-weight: 500;
      }

      .example-items {
        display: flex;
        flex-direction: column;
        gap: 12px;
      }

      .example-item {
        padding: 12px 16px;
        background: #f7f7f8;
        border-radius: 8px;
        display: flex;
        align-items: center;
        cursor: pointer;
        transition: all 0.3s;
        border: 1px solid transparent;

        span {
          margin-left: 10px;
        }

        &:hover {
          background: #e5f3ee;
          border-color: #008f5f;
          transform: translateY(-2px);
        }
      }
    }
  }

  .chat-content {
    margin-bottom: 20px;

    &:last-child {
      margin-bottom: 0;
    }
  }

  .chat-my {
    background: #008f5f;
    border-radius: 10px 2px 10px 10px;
    color: white;
    padding: 10px 16px;
    box-sizing: border-box;
    display: inline-block;
    max-width: 100%;
    line-height: 1.6;
    word-break: break-word;
  }

  .chat-ai {
    background: #f7f7f8;
    border-radius: 2px 10px 10px 10px;
    padding: 10px 16px;
    box-sizing: border-box;
    display: inline-block;
    max-width: 100%;
    white-space: pre-wrap;
    line-height: 1.6;
    word-break: break-word;
    color: #333;
  }
}

/* 输入区域样式 */
.input-container {
  position: relative;
  margin-top: 10px;

  .el-input {
    margin-bottom: 0;
  }

  .send-button {
    position: absolute;
    bottom: 10px;
    right: 10px;
    z-index: 10;
    border-radius: 6px;
    transition: all 0.3s ease;
    cursor: pointer;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 2px 8px rgba(0, 140, 95, 0.2);
    }

    &:active {
      transform: translateY(0);
    }
  }
}

/* 滚动条样式 */
.chat {
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 3px;

    &:hover {
      background: #ccc;
    }
  }

  &::-webkit-scrollbar-track {
    background: transparent;
  }
}

.disabledStyle {
  color: #a0a0a0; /* 浅色文字 */
  opacity: 0.6; /* 降低透明度 */
  cursor: not-allowed; /* 鼠标悬停时显示禁用图标 */
}

.stopped-message {
  display: flex;
  align-items: center;
  margin-top: 8px;
  font-size: 12px;
  color: #909399;

  .stopped-icon {
    margin-right: 6px;
    color: #e6a23c;
  }
}
</style>
